package com.jph.simple;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NotificationCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import adapter.OptionAdapter;
import container.ChatManager;
import container.FriendManager;
import container.PlayerContainer;
import model.Option;
import socket.ServerSession;


/**
 * A simple {@link Fragment} subclass.
 */
public class SelfFragment extends Fragment {

    private ImageView mSelfImageIv;
    private TextView mSelfNameTv;

    private ListView mOptionsLv;

    private List<Option> mOptionList = new ArrayList<>();


    public SelfFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_self, container, false);

        initView(v);

        mSelfImageIv.setImageDrawable(getResources().getDrawable(R.drawable.self));
        mSelfNameTv.setText(PlayerContainer.getInstance().getName());

        initOptions();

        OptionAdapter chatLogAdapter = new OptionAdapter(getActivity(),
                R.layout.option_item, mOptionList);
        mOptionsLv.setAdapter(chatLogAdapter);

        mOptionsLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {  //点击每一行的点击事件

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position,
                                    long id) {
                Option option= mOptionList.get(position - 1);

                if (option.getName().equals(getResources().getString(R.string.setting))){

//                    Intent intent = new Intent();
//                    intent.setClass(getActivity(), PlayerChatActivity.class);
//
//                    startActivity(intent);
//                    getActivity().finish();

                } else if (option.getName().equals(getResources().getString(R.string.exit))){
                    exit();
                } else if (option.getName().equals(getResources().getString(R.string.logoff))){
                    logoff();
                }
            }
        });

        return v;
    }

    private void initView(View v){
        mSelfImageIv = (ImageView) v.findViewById(R.id.ivSelfImage);
        mSelfNameTv = (TextView) v.findViewById(R.id.tvSelfName);

        mOptionsLv = (ListView) v.findViewById(R.id.lvOptions);

        mOptionsLv.addHeaderView(new ViewStub(getContext()));
        mOptionsLv.addFooterView(new ViewStub(getContext()));
    }

    private void initOptions(){
        mOptionList.clear();
        mOptionList.add(new Option(R.drawable.setting, getResources().getString(R.string.setting)));
        mOptionList.add(new Option(R.drawable.exit, getResources().getString(R.string.exit)));
        mOptionList.add(new Option(R.drawable.logoff, getResources().getString(R.string.logoff)));
    }

    private void exit(){
        getActivity().finish();
        System.exit(0);
    }

    private void logoff(){

        ServerSession.getInstance().playerLogOff();

        Intent intent = new Intent();
        intent.setClass(getActivity(), LogonActivity.class);

        startActivity(intent);
        getActivity().finish();

        FriendManager.getInstance().destroy();
        ChatManager.getInstance().destroy();
    }

}
